package com.richman.view;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.richman.controller.BetClient;
import com.richman.ps38.entity.BetAdyData;
import com.richman.ps38.entity.Event;
import com.richman.ps38.entity.SendData;
import com.richman.utils.Common;
import com.richman.utils.SoundThread;

import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;


public class BetWindow extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 16325L;
	private JPanel contentPane;
	private JTextField hgBetAmtTextField;
	private JTextField ps38BetAmtTextField;
	
	private JLabel betTypelb;
	private JLabel betLeglb;
	private JLabel betMatchlb;
	private JLabel hgTeamlb;
	private JLabel ps38Teamlb;
	private JLabel hgMinbetlb;
	private JLabel ps38Minbetlb;
	private JLabel hgMaxbetlb;
	private JLabel ps38Maxbetlb;
	private JLabel hgBetAmtlb;
	private JLabel ps38BetAmtlb;
	private JLabel hgpankoulb;
	private JLabel ps38pankoulb;
	
	private JLabel messagelb;


	private SendData betgame;
	private String bettype = "";
	private String updown = "";
	
	
	private String hgpankoustr = "";
	private String ps38pankoustr = "";
	
	private String hgbetratio = "";
	
	
	private JLabel mergeRatio;
	
	private long lastbetsuccessTime = 0L;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BetWindow frame = new BetWindow();
					frame.setTitle("投注");
					frame.betTypelb.setText("单式全场让球");
					
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public BetWindow() {
		
		setTitle("投注窗口");
		
		setResizable(false);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);  
		setBounds(100, 100, 598, 599);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		betTypelb = new JLabel("New label");
		betTypelb.setBounds(23, 27, 213, 15);
		betTypelb.setFont(new Font("宋体", Font.PLAIN, 18));
		contentPane.add(betTypelb);
		
		betLeglb = new JLabel("New label");
		betLeglb.setBounds(23, 66, 213, 15);
		betLeglb.setFont(new Font("宋体", Font.PLAIN, 18));
		contentPane.add(betLeglb);
		
		betMatchlb = new JLabel("New label");
		betMatchlb.setBounds(23, 103, 213, 15);
		betMatchlb.setFont(new Font("宋体", Font.PLAIN, 18));
		contentPane.add(betMatchlb);
		
		hgTeamlb = new JLabel("New label");
		hgTeamlb.setFont(new Font("宋体", Font.PLAIN, 18));
		hgTeamlb.setBounds(23, 156, 213, 15);
		contentPane.add(hgTeamlb);
		
		ps38Teamlb = new JLabel("New label");
		ps38Teamlb.setBounds(307, 156, 213, 15);
		ps38Teamlb.setFont(new Font("宋体", Font.PLAIN, 18));
		contentPane.add(ps38Teamlb);
		
		hgMinbetlb = new JLabel("New label");
		hgMinbetlb.setBounds(23, 237, 213, 15);
		hgMinbetlb.setFont(new Font("宋体", Font.PLAIN, 18));
		contentPane.add(hgMinbetlb);
		
		ps38Minbetlb = new JLabel("New label");
		ps38Minbetlb.setBounds(307, 237, 213, 15);
		ps38Minbetlb.setFont(new Font("宋体", Font.PLAIN, 18));
		contentPane.add(ps38Minbetlb);
		
		hgMaxbetlb = new JLabel("New label");
		hgMaxbetlb.setBounds(23, 282, 213, 15);
		hgMaxbetlb.setFont(new Font("宋体", Font.PLAIN, 18));
		contentPane.add(hgMaxbetlb);
		
		ps38Maxbetlb = new JLabel("New label");
		ps38Maxbetlb.setBounds(307, 282, 213, 15);
		ps38Maxbetlb.setFont(new Font("宋体", Font.PLAIN, 18));
		contentPane.add(ps38Maxbetlb);
		
		
		
		
		hgpankoulb = new JLabel("New label");
		hgpankoulb.setBounds(23, 199, 213, 15);
		hgpankoulb.setFont(new Font("宋体", Font.PLAIN, 18));
		contentPane.add(hgpankoulb);
		
		ps38pankoulb = new JLabel("New label");
		ps38pankoulb.setBounds(307, 199, 213, 15);
		ps38pankoulb.setFont(new Font("宋体", Font.PLAIN, 18));
		contentPane.add(ps38pankoulb);
		
		JButton refreshBtn = new JButton("刷新");
		refreshBtn.setBounds(23, 385, 109, 34);
		contentPane.add(refreshBtn);
		
		
		
		mergeRatio = new JLabel("New label");
		mergeRatio.setBounds(23, 452, 213, 15);
		mergeRatio.setFont(new Font("宋体", Font.PLAIN, 18));
		contentPane.add(mergeRatio);
		
		
		refreshBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				refreshOdds();
				
			}
		});
		
		
		
		
		
		
		hgBetAmtlb = new JLabel("投注金额：");
		hgBetAmtlb.setBounds(23, 335, 90, 15);
		hgBetAmtlb.setFont(new Font("宋体", Font.PLAIN, 18));
		contentPane.add(hgBetAmtlb);
		
		ps38BetAmtlb = new JLabel("投注金额：");
		ps38BetAmtlb.setBounds(307, 335, 90, 15);
		ps38BetAmtlb.setFont(new Font("宋体", Font.PLAIN, 18));
		contentPane.add(ps38BetAmtlb);
		
		hgBetAmtTextField = new JTextField();
		hgBetAmtTextField.setBounds(130, 332, 160, 30);
		hgBetAmtTextField.setFont(new Font("宋体", Font.PLAIN, 18));
		contentPane.add(hgBetAmtTextField);
		hgBetAmtTextField.setColumns(10);
		
		ps38BetAmtTextField = new JTextField();
		ps38BetAmtTextField.setColumns(10);
		ps38BetAmtTextField.setFont(new Font("宋体", Font.PLAIN, 18));
		ps38BetAmtTextField.setBounds(400, 332, 160, 30);
		contentPane.add(ps38BetAmtTextField);
		
		JButton betBtn = new JButton("投注");
		betBtn.setBounds(297, 385, 109, 34);
		contentPane.add(betBtn);
		
		
		betBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				
				try {
					
					if(hgpankoustr.equals("")|| ps38pankoustr.equals("")) {
						messagelb.setText("请先刷新赔率");
						messagelb.setForeground(new Color(255,0,0));
						return;
					}
					
					if(Double.parseDouble(hgpankoustr) - Double.parseDouble(ps38pankoustr) < 0.001) {	
						dobet();
						return;
					}
					
				}catch(Exception ex) {
					ex.printStackTrace();
				}
				
				messagelb.setText("投注失败");
				messagelb.setForeground(new Color(255,0,0));
				
			}
		});
		
		
		messagelb = new JLabel("New label");
		messagelb.setBounds(23, 495, 399, 21);
		messagelb.setFont(new Font("宋体", Font.PLAIN, 18));
		contentPane.add(messagelb);
		
		
		JButton exitButton = new JButton("退出");
		exitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				setVisible(false);
				
			}
		});
		exitButton.setBounds(431, 494, 109, 34);
		contentPane.add(exitButton);

		
	}

	
	/*
	 * type:	单式全场让球
	 * updown:	1代表 HG主大+PS38客小符合条件。2代表HG客小+PS38主大符合条件
	 * betgame: 某异常比赛
	 */
	public boolean setgame(String bettypearg, String updownarg, SendData betgamearg) {
		
		
		bettype = bettypearg;
		updown = updownarg;

		betgame = betgamearg;
		

		
		
		hgpankoustr = "";
		ps38pankoustr = "";
		
		hgbetratio = "";
		
		
		return refreshOdds();

	}
	
	
	public void setmegerRatioText(String str) {
		mergeRatio.setText(str);
	}
	
	public boolean refreshOdds() {
		
		try {
			
			betTypelb.setText(bettype);
			betLeglb.setText(betgame.hgevent.leagueName);
			betMatchlb.setText(betgame.hgevent.homeTeam + "-vs-" + betgame.hgevent.awayTeam);
			
			
			
			
			
			
			hgTeamlb.setText("");
			
			
			hgMinbetlb.setText("");
			hgMaxbetlb.setText("");
			
			
			ps38Teamlb.setText("");
			
			ps38Minbetlb.setText("");
			ps38Maxbetlb.setText("");
			
			
			
			
			
			
			Map<String, String> details = null;
			
			Map<String, String> ps38details = null;
			
			String hgTeamlbStr = "";
			String ps38Teamlbstr = "";
			
			
			String ps38typedetail = "";
			
			String ps38betid = "";
			
			String wtypestr = "";
			
			String typestr = "";
			
			String gnumstr = "";
			
			String strongstr = "";
			
			String ps38betpankou = "";
			
			String gidstr = "";
			

			if(bettype.contains("半场")) {
				wtypestr = "H";	
				
				gidstr = betgame.hgevent.hgid;
				
				ps38betid = betgame.event.halfBetID;
				
			}else {
				
				gidstr = betgame.hgevent.gid;
				
				if(bettype.contains("大小")) {
					ps38betid = betgame.event.wholeDXPBetID;
				}else {
					ps38betid = betgame.event.wholeRQPBetID;
				}
				
				
				
			}
			
			
			//滚球大小
			if(bettype.contains("滚球") && bettype.contains("大小")) {
				wtypestr += "R";
			}
			

			
			if(bettype.contains("让球")) {
				
				wtypestr += "R";
				
				if(updown.contains("1")) {
					hgTeamlbStr = "皇冠:" + betgame.hgevent.homeTeam + "@";
					ps38Teamlbstr = "PS38:" + betgame.event.awayTeam + "@";
					ps38typedetail = "客";
					
					typestr = "H";
					gnumstr = betgame.hgevent.gnumh;
					
				}else {
					hgTeamlbStr = "皇冠:" + betgame.hgevent.awayTeam + "@";
					ps38Teamlbstr = "PS38:" + betgame.event.homeTeam + "@";
					ps38typedetail = "主";
					
					typestr = "C";
					gnumstr = betgame.hgevent.gnumc;
				}
				
				
				
				
				if(bettype.contains("全场")) {
					ps38betpankou = betgame.event.wholerqppankou;
					strongstr = betgame.hgevent.strong;
				}else {
					ps38betpankou = betgame.event.halfrqppankou;
					strongstr = betgame.hgevent.hstrong;
				}
				

				
			}else {
				
				wtypestr += "OU";
				
				if(updown.contains("1")) {
					hgTeamlbStr = "皇冠:" +  "大@";
					ps38Teamlbstr = "PS38:" +  "小@";
					ps38typedetail = "小";
					
					typestr = "C";
					gnumstr = betgame.hgevent.gnumc;

				}else {
					hgTeamlbStr = "皇冠:" + "小@";
					ps38Teamlbstr = "PS38:" + "大@";
					ps38typedetail = "大";
					
					typestr = "H";
					gnumstr = betgame.hgevent.gnumh;
				}
				
				
				if(bettype.contains("全场")) {
					ps38betpankou = betgame.event.wholeDXPPankou;
				}else {
					ps38betpankou = betgame.event.halfDXPPankou;
				}
				
				
			}
			

			//滚球让球
			if(bettype.contains("滚球") && bettype.contains("让球")) {
				wtypestr += "E";
			}
			
			
			if(!ps38pankoustr.equals("")) {
				ps38betpankou = ps38pankoustr;
			}
			
			
			
			if(BetClient.isInbet == true) {
				messagelb.setText("投注中，请稍后刷新");
				
				messagelb.setForeground(new Color(255,0,0));
				return false;
			}
			
			

			
			details = BetClient.betCtrMng.updateRatio(gidstr, typestr, gnumstr, strongstr, wtypestr + typestr, wtypestr);

			ps38details = BetClient.betCtrMng.updatePS38Ratio(ps38betid, Long.toString(betgame.event.eventId), bettype, ps38typedetail, ps38betpankou);

			
			if(details != null && ps38details != null) {
				
				hgTeamlb.setText(hgTeamlbStr + details.get("ratio"));
				
				
				
				
				hgMinbetlb.setText("最小投注金额："+ details.get("minbet"));
				hgMaxbetlb.setText("最大投注金额："+ details.get("maxbet"));
				
				
				ps38Teamlb.setText(ps38Teamlbstr + ps38details.get("ratio"));
				
				ps38Minbetlb.setText(ps38details.get("minbet"));
				ps38Maxbetlb.setText(ps38details.get("maxbet"));
				
				
				
				if(Double.parseDouble(ps38details.get("maxbet"))   < BetClient.mainWnd.getstartBetGold() ){
					Common.logOut("平博最大投注值小于起投标准");
					return false;
				}
				

				messagelb.setText("赔率刷新成功");
				messagelb.setForeground(new Color(0,255,0));
				
				
				double ps38ratio = Double.parseDouble(ps38details.get("ratio"));
				
				double dhgbetratio = Double.parseDouble(details.get("ratio"));
				
				if(dhgbetratio<0.0) {
					dhgbetratio = 2.0 + dhgbetratio;
				}
				
				if(ps38ratio < 0) {
					ps38ratio = 2.0+ ps38ratio;
				}
				
				String mergeRatio = String.format("%.3f", dhgbetratio + ps38ratio);
				
				setmegerRatioText(mergeRatio);
				
				hgpankoulb.setText("皇冠盘口:" + details.get("pankou"));
				
				ps38pankoulb.setText("ps38盘口:" + ps38details.get("pankou"));
				
				//DaShuiClient.showtext += Common.getcurrentTimeStr() + "HG盘口:" + details.get("pankou") + "   ps38盘口" + ps38details.get("pankou") + "\n";
				
				hgpankoustr =  details.get("pankou");
				ps38pankoustr = ps38details.get("pankou");
				
				
				int hgbetgold = Integer.parseInt(details.get("maxbet").replace(",", ""));
				
				int hgsetbetgold = BetClient.mainWnd.gethgBetAmount();
				
				
				
				
				
				//add
				
				int hgmaxbetgold = Integer.parseInt(details.get("maxbet").replace(",", ""));
				
				//int hgsetbetgold = DaShuiClient.mainWnd.gethgBetAmount();
				
				
				
				//int hgbetgold = 0;
				
				
				
				//double ps38betgold = DaShuiClient.mainWnd.gethgBetAmount() * DaShuiClient.mainWnd.getps38betRatio();
				
				
				
				
				double ps38betgold = 0.0;//DaShuiClient.mainWnd.gethgBetAmount() * DaShuiClient.mainWnd.getps38betRatio();
				
//				if(Double.parseDouble(ps38details.get("maxbet"))   < DaShuiClient.mainWnd.getstartBetGold() ){
//					Common.logOut("平博最大投注值小于起投标准");
//					return;
//				}
				
				
				
				if(BetClient.mainWnd.getbps38betAmount()== true) {
					ps38betgold = BetClient.mainWnd.getps38betAmount();
					
					if(hgsetbetgold < hgmaxbetgold) {
						hgbetgold = hgsetbetgold;
					}else {
						hgbetgold = hgmaxbetgold;
					}
					

					
					
				}else {
					
					ps38betgold = Double.parseDouble(ps38details.get("maxbet"));
					
					hgbetgold = (int)(ps38betgold/1000)*1000;
					
					
					
					if(hgbetgold > hgmaxbetgold) {
						hgbetgold = hgmaxbetgold;
					}
					
					
					if(hgbetgold > BetClient.mainWnd.gethgmaxbet()) {
						hgbetgold = BetClient.mainWnd.gethgmaxbet();
					}
					
					ps38betgold = (hgbetgold/100);
					
				}
				
				
				

				
				if(ps38betgold < Double.parseDouble(ps38details.get("minbet"))) {
					ps38betgold = Double.parseDouble(ps38details.get("minbet")) + 1.0;
				}
				
				//add end
				
				
				
				
//				if(hgsetbetgold < hgbetgold) {
//					hgbetgold = hgsetbetgold;
//				}
				
				//if(hgBetAmtTextField.getText().equals("")) {
					hgBetAmtTextField.setText(Integer.toString(hgbetgold));
				//}
				
				
				//double ps38betgold = 0.0;//DaShuiClient.mainWnd.gethgBetAmount() * DaShuiClient.mainWnd.getps38betRatio();
				
//				if(DaShuiClient.mainWnd.getbps38betAmount()== true) {
//					ps38betgold = DaShuiClient.mainWnd.getps38betAmount();
//				}else {
//					ps38betgold = DaShuiClient.mainWnd.gethgBetAmount() * DaShuiClient.mainWnd.getps38betRatio();
//				}
//				
//				
//				if(ps38betgold < Double.parseDouble(ps38details.get("minbet"))) {
//					ps38betgold = Double.parseDouble(ps38details.get("minbet"));
//				}
				
				//if(ps38BetAmtTextField.getText().equals("")) {
					ps38BetAmtTextField.setText(Double.toString(ps38betgold));
				//}
				


			}else {
				messagelb.setText("赔率刷新失败");
				
				messagelb.setForeground(new Color(255,0,0));
			}
			
			
		}catch(Exception e) {
			
			e.printStackTrace();
			
			messagelb.setText("赔率刷新失败");
			
			messagelb.setForeground(new Color(255,0,0));
			
		}
		
		return true;
		

	}
	
	
	
	
	
	
	public void dobet() {
		try {
			
			
			
			if(System.currentTimeMillis() - lastbetsuccessTime < BetClient.mainWnd.getmanualBetSuccessTime()*1000) {
				
				messagelb.setText("投注过于频繁，请稍后投注");
				
				messagelb.setForeground(new Color(255,0,0));
				
				return;
				
			}
			
			
			
			
			
			String hgTeamlbStr = "";
			String ps38Teamlbstr = "";
			
			
			String ps38typedetail = "";
			
			String ps38betid = "";
			
			String wtypestr = "";
			
			String typestr = "";
			
			String gnumstr = "";
			
			String strongstr = "";
			
			String ps38betpankou = "";
			
			String gidstr = "";
			
			
			if(bettype.contains("半场")) {
				wtypestr = "H";	
				
				gidstr = betgame.hgevent.hgid;
				
				ps38betid = betgame.event.halfBetID;
				
			}else {
				
				gidstr = betgame.hgevent.gid;
				
				if(bettype.contains("大小")) {
					ps38betid = betgame.event.wholeDXPBetID;
				}else {
					ps38betid = betgame.event.wholeRQPBetID;
				}
				
			}
			
			
			//滚球大小
			if(bettype.contains("滚球") && bettype.contains("大小")) {
				wtypestr += "R";
			}
			

			BetAdyData bad = new BetAdyData();
			
			
			if(bettype.contains("让球")) {
				
				wtypestr += "R";
				
				if(updown.contains("1")) {
					hgTeamlbStr = "皇冠:" + betgame.hgevent.homeTeam + "@";
					ps38Teamlbstr = betgame.event.awayTeam + "@";
					ps38typedetail = "客";
					
					typestr = "H";
					gnumstr = betgame.hgevent.gnumh;
					
				}else {
					hgTeamlbStr = "皇冠:" + betgame.hgevent.awayTeam + "@";
					ps38Teamlbstr = betgame.event.homeTeam + "@";
					ps38typedetail = "主";
					
					typestr = "C";
					gnumstr = betgame.hgevent.gnumc;
				}
				
				
				
				
				if(bettype.contains("全场")) {
					ps38betpankou = betgame.event.wholerqppankou;
					strongstr = betgame.hgevent.strong;
				}else {
					ps38betpankou = betgame.event.halfrqppankou;
					strongstr = betgame.hgevent.hstrong;
				}
				

				
			}else {
				
				wtypestr += "OU";
				
				if(updown.contains("1")) {
					hgTeamlbStr = "皇冠:" +  "大@";
					ps38Teamlbstr = "小@";
					ps38typedetail = "小";
					
					typestr = "C";
					gnumstr = betgame.hgevent.gnumc;

				}else {
					hgTeamlbStr = "皇冠:" + "小@";
					ps38Teamlbstr = "大@";
					ps38typedetail = "大";
					
					typestr = "H";
					gnumstr = betgame.hgevent.gnumh;
				}
				
				
				if(bettype.contains("全场")) {
					ps38betpankou = betgame.event.wholeDXPPankou;
				}else {
					ps38betpankou = betgame.event.halfDXPPankou;
				}
				
				
			}
			

			//滚球让球
			if(bettype.contains("滚球") && bettype.contains("让球")) {
				wtypestr += "E";
			}
			
			
			
			if(!ps38pankoustr.equals("")) {
				ps38betpankou = ps38pankoustr;
			}
			
			
			
			
			
			Map<String, String> oddUpdres = BetClient.betCtrMng.updateRatio(gidstr, typestr, gnumstr, strongstr, wtypestr + typestr, wtypestr);
			
			Map<String, String> ps38oddUpdres = BetClient.betCtrMng.updatePS38Ratio(ps38betid, Long.toString(betgame.event.eventId), bettype, ps38typedetail, ps38betpankou);
			
			SimpleDateFormat dfSec = new SimpleDateFormat("HH:mm:ss");
			
			if(oddUpdres == null || ps38oddUpdres == null) {
				
				System.out.println(dfSec.format(System.currentTimeMillis()) + ":获取赔率失败");
				
				return;
			}
			
			
			String betres = "";
			
			Double hgratio = Double.parseDouble(oddUpdres.get("ratio"));
			Double ps38ratio = Double.parseDouble(ps38oddUpdres.get("ratio"));
			
			int hgbetgold = Integer.parseInt(hgBetAmtTextField.getText());
			
			int ps38betgold = (int)Double.parseDouble(ps38BetAmtTextField.getText());
			
			
			
			int hgminbetgold = Integer.parseInt(oddUpdres.get("minbet"));
			int hgmaxbetgold = Integer.parseInt(oddUpdres.get("maxbet").replace(",",""));

			if(hgbetgold < hgminbetgold || hgbetgold > hgmaxbetgold) {
				messagelb.setText("皇冠投注金额不在投注范围");
				
//				messagelb.setForeground(new Color(255,0,0));
//				betres  = "错过盘口";
//				
//				bad.hgbetres = betres;
//				bad.ps38betres = betres;
//				
//				DaShuiClient.betCtrMng.updateBetLogData(bad);
				
				return;
			}

			
			bad.hgps38 = updown;
			
			bad.currenttime = dfSec.format(System.currentTimeMillis());
			bad.league = betgame.hgevent.leagueName;
			bad.teams = betgame.hgevent.homeTeam + "-vs-" + betgame.hgevent.awayTeam;
			bad.time = betgame.hgevent.eventTime;
			
			bad.score = betgame.hgevent.homeTeamScore + ":" + betgame.hgevent.awayTeamScore;
			
			
			bad.hgeventid = betgame.hgevent.eventId;
			
			bad.bettype = bettype;
			
			bad.hgbettype = hgTeamlbStr.replace("皇冠:", "") + oddUpdres.get("ratio");
			bad.betpankou = ps38betpankou;
			bad.hgbettime = Long.toString(System.currentTimeMillis());
			//bad.hgbettime = dfSec.format(System.currentTimeMillis());
			
			bad.hgbetres = "";
			
			bad.ticketid = "";
			bad.gid = gidstr;
			bad.hgbetgold = Integer.toString(hgbetgold);
			
			
			
			double hgratio1 = hgratio;
			if(hgratio < 0) {
				hgratio1 = 2.0 + hgratio;
			}
			
			double ps38ratio1 = ps38ratio;
			if(ps38ratio< 0) {
				ps38ratio1 = 2.0 + ps38ratio;
			}
			
			
			bad.mergeratio = String.format("%.3f", hgratio1 + ps38ratio1);
			
			bad.ps38eventid = Long.toString(betgame.event.eventId);
			
			
			bad.wagerId = "";
			bad.ps38betres = "";
			//bad.ps38bettime = dfSec.format(System.currentTimeMillis());
			bad.ps38bettype = ps38Teamlbstr + ps38oddUpdres.get("ratio");
			bad.ps38betgold = Integer.toString(ps38betgold);
			
			if(!Common.isNum(hgBetAmtTextField.getText()) || !Common.isNum(ps38BetAmtTextField.getText())) {
				messagelb.setText("请填写正确的投注金额");
				
				messagelb.setForeground(new Color(255,0,0));
				return;
			}
			
			
			
			
			
			//todo  对赔率进行分析，看是否还能进行投注
			if(!isInRatioRange(hgratio, ps38ratio)) {
				messagelb.setText("赔率已不符合设置条件，无法投注");
				
				messagelb.setForeground(new Color(255,0,0));
				
				betres  = "赔率不符条件";
				
				bad.hgbetres = betres;
				bad.ps38betres = betres;
				
				//发出声音
	    		if(SoundThread.inPlay == false){
					SoundThread st = new SoundThread();
					SoundThread.status = 1;
					st.start();
				}
				
				BetClient.betCtrMng.updateBetLogData(bad);
				
				return;
			}
			
			Double hgpankou = Double.parseDouble(oddUpdres.get("pankou"));
			Double ps38pankou = Double.parseDouble(ps38oddUpdres.get("pankou"));
			
			
			if(Math.abs(hgpankou - ps38pankou) > 0.01) {
				messagelb.setText("皇冠，ps38盘口不一致，无法投注");
				
				messagelb.setForeground(new Color(255,0,0));
				
				
				betres  = "盘口不一致";
				
				bad.hgbetres = betres;
				bad.ps38betres = betres;
				
				//发出声音
	    		if(SoundThread.inPlay == false){    			
					SoundThread st = new SoundThread();
					SoundThread.status = 1;
					st.start();
				}
				
				BetClient.betCtrMng.updateBetLogData(bad);
				
				return;
			}

			
			int ps38minbetgold = (int)Double.parseDouble(ps38oddUpdres.get("minbet"));
			int ps38maxbetgold = (int)Double.parseDouble(ps38oddUpdres.get("maxbet"));
			
			if(ps38betgold < ps38minbetgold || ps38betgold > ps38maxbetgold) {
				messagelb.setText("PS38投注金额不在投注范围");
				
				messagelb.setForeground(new Color(255,0,0));
				return;
			}
			
			
			
			
			BetClient.isInbet = true;
			

			int refreshdelaybettime = BetClient.mainWnd.getrefreshdelaybettime();
			
			if(refreshdelaybettime > 0 ) {
				Thread.sleep(refreshdelaybettime * 1000);
			}
			
			
			
			Map<String,String> res = BetClient.betCtrMng.hgdobet(oddUpdres.get("betParam"), hgbetgold);
			
			
			//String[] res = {"1", ""};
			
			if(res == null) {
				messagelb.setText("皇冠账号尚未登录");
				
				
				System.out.println(dfSec.format(System.currentTimeMillis())  + ": 皇冠账号尚未登录,投注返回null:" + bad.toString());
				bad.hgbetres = "账号掉线";
				BetClient.betCtrMng.updateBetLogData(bad);
				
				
				messagelb.setForeground(new Color(255,0,0));
			}else {
				
				if(res.get("status").equals("1") || res.get("status").equals("0")) {
					
					
					
					lastbetsuccessTime = System.currentTimeMillis();
					
					bad.league = betgame.hgevent.leagueName;
					bad.teams = betgame.hgevent.homeTeam + "-vs-" + betgame.hgevent.awayTeam;
					bad.time = betgame.hgevent.eventTime;
					
					
					
					
					bad.hgeventid = betgame.hgevent.eventId;
					
					bad.bettype = bettype;
					
					bad.hgbettype = res.get("betteam") + "@ " + res.get("betratio");
					bad.betpankou = res.get("betpankou");
					//bad.hgbettime = res.get("bettime");
					
					bad.hgbetres = res.get("status");
					
					bad.ticketid = res.get("ticketid");
					bad.gid = gidstr;
					bad.hgbetgold = Integer.toString(hgbetgold);
					
					
					//bet ps38
					Map<String, String> rval = BetClient.betCtrMng.ps38dobet(ps38oddUpdres, ps38betgold);
					
					//bad.mergeratio = String.format("%.3f", hgratio + ps38ratio);
					
					bad.ps38eventid = Long.toString(betgame.event.eventId);
					
					
					
					if(rval.get("status").equals("1")) {
						
						
						
						bad.wagerId = rval.get("wagerId");
						bad.ps38betres = "1";
						//bad.ps38bettime = rval.get("bettime");
						bad.ps38bettype = ps38Teamlbstr + rval.get("ratio");
						bad.ps38betgold = Integer.toString(ps38betgold);
						
						if(res.get("status").equals("0")) {
							messagelb.setText("皇冠投注待确认, ps38投注成功");
						}else {
							messagelb.setText("皇冠投注成功, ps38投注成功");
						}

						messagelb.setForeground(new Color(0,255,0));
						
						
						
						
					}else if(rval.get("status").equals("0")){
						
						bad.wagerId = rval.get("wagerId");
						bad.ps38betres = "0";
					//	bad.ps38bettime = rval.get("bettime");
						bad.ps38bettype = ps38Teamlbstr + rval.get("ratio");
						bad.ps38betgold = Integer.toString(ps38betgold);
						
						if(res.get("status").equals("0")) {
							messagelb.setText("皇冠投注待确认, ps38投注待确认");
						}else {
							messagelb.setText("皇冠投注成功, ps38投注待确认");
						}
						
						
						
						messagelb.setForeground(new Color(0,255,0));
						
					}else {
						
						if(res.get("status").equals("0")) {
							messagelb.setText("皇冠投注待确认, ps38投注失败");
						}else {
							messagelb.setText("皇冠投注成功, ps38投注失败");
						}

						
						messagelb.setForeground(new Color(255,0,0));
					}
					
					
					
					//update 
					BetClient.betCtrMng.updateBetAdyData(bad);
					BetClient.betCtrMng.updateBetLogData(bad);
					
					
					//发出声音
		    		if(SoundThread.inPlay == false){    			
						SoundThread st = new SoundThread();
						SoundThread.status = 0;
						st.start();
					}
					
					
				}else{
					messagelb.setText("皇冠投注失败");
					
					messagelb.setForeground(new Color(255,0,0));
					
					bad.hgbetres = res.get("status");
					
					BetClient.betCtrMng.updateBetLogData(bad);
					
					//发出声音
		    		if(SoundThread.inPlay == false){    			
						SoundThread st = new SoundThread();
						SoundThread.status = 1;
						st.start();
					}
				}
				
				
				
			}
			
			
			
		}catch(Exception e) {
			
			BetClient.isInbet = false;
			
			
			e.printStackTrace();
			
		}
		
		BetClient.isInbet = false;
		
	}
	
	
	
	private boolean isInRatioRange(double hgodd, double ps38odd) {
		
		double sideMinodd = BetClient.mainWnd.getsideMinodd();
		double sumMinodd = BetClient.mainWnd.getsumMinodd();
		
		double sideMaxodd = BetClient.mainWnd.getsideMaxodd();
		double sumMaxodd = BetClient.mainWnd.getsumMaxodd();
		
		DecimalFormat df = new DecimalFormat("#.000");
		
		if(hgodd < 0) {
			hgodd = Double.parseDouble(df.format(2.0 + hgodd));
		}
		
		
		
		
		if(ps38odd < 0.0) {
			ps38odd = Double.parseDouble(df.format(2.0 + ps38odd));
		}
		
		if(hgodd + ps38odd < BetClient.mainWnd.getbetodd()) {
			return false;
		}
		
		
		double betodd = BetClient.mainWnd.getbetodd();
		
		
		if(hgodd >= sideMinodd && ps38odd >= sideMinodd && (hgodd + ps38odd) >= betodd && hgodd <= sideMaxodd && ps38odd <= sideMaxodd && (hgodd+ps38odd) <= sumMaxodd) {
			return true;
		}
		
		return false;
	}
	
	
	
}
